package com.socket;


import com.alibaba.druid.pool.DruidDataSource;
import com.socket.listener.SpringAfterInitListener;
import com.socket.netty.NettyServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;


/**
 * Created by kaddddd on 2017/7/5 0005.
 */
@SpringBootApplication
@ServletComponentScan // 注意要加上@ServletComponentScan注解，否则Servlet无法生效
@ComponentScan({"com.socket"})
@ImportResource("classpath*:/applicationContext.xml")
@EnableTransactionManagement //开启事物的注解
public class Application extends SpringBootServletInitializer{
    //DataSource配置
//    @Bean
//    @ConfigurationProperties(prefix="spring.datasource")
//    public DataSource dataSource() {
//        return new DruidDataSource();
//    }



//    @Override
//    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//        return builder.sources(Application.class);
//    }

    //提供SqlSeesion
//    @Bean
//    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
//
//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//        sqlSessionFactoryBean.setDataSource(dataSource());
//
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//
//        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml"));
//
//        return sqlSessionFactoryBean.getObject();
//    }

//    @Bean
//    public PlatformTransactionManager transactionManager() {
//        return new DataSourceTransactionManager(dataSource());
//    }

    /**
     * Main Start
     */
    public static void main(String[] args) {
        //启动
        SpringApplication springApplication = new SpringApplication();
        springApplication.addListeners(new SpringAfterInitListener());
        springApplication.run(Application.class, args);//挂起主线程

        try {
            NettyServer.build(8085,5,5).start().waitToClose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
